package com.mega.fe.util.client;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.font.FontManager;
import net.minecraft.client.gui.font.FontSet;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.util.FormattedCharSequence;
import org.joml.Matrix4f;

import java.awt.*;

public class MegaFont {
    public static FontManager fontManager = Minecraft.getInstance().fontManager;
    public static Font FantasyFont = new Font((p_284586_) -> {
        return fontManager.fontSets.getOrDefault(fontManager.getActualId(p_284586_), fontManager.missingFontSet);
    }, false) {
        public long milliTime() {
            return System.nanoTime() / 1000000L;
        }

        public double rangeRemap(double value, double low1, double high1, double low2, double high2) {
            return low2 + (value - low1) * (high2 - low2) / (high1 - low1);
        }

        @Override
        public int drawInBatch(String p_272751_, float p_272661_, float p_273129_, int p_273272_, boolean p_273209_, Matrix4f p_272940_, MultiBufferSource p_273017_, DisplayMode p_272608_, int p_273365_, int p_272755_) {
            return super.drawInBatch(p_272751_, p_272661_, p_273129_, p_273272_, p_273209_, p_272940_, p_273017_, p_272608_, p_273365_, p_272755_);
        }

        @Override
        public int drawInBatch(FormattedCharSequence p_273262_, float p_273006_, float p_273254_, int p_273375_, boolean p_273674_, Matrix4f p_273525_, MultiBufferSource p_272624_, DisplayMode p_273418_, int p_273330_, int p_272981_) {
            float colorr = (float) milliTime() / 400.0F % 1.0F;
            float colorrStep = (float) rangeRemap(
                    Math.sin(((float) milliTime() / 200.0F)) % 6.28318D, -0.9D, 2.5D, 0.025D, 0.15D);
            float posX = p_273006_;
            String drawText = FontTextBuilder.formattedCharSequenceToString(p_273262_);
            if (drawText.equals("「梦幻 • 终点」 ")) {
                for (int i = 0; i < drawText.length(); i++) {
                    float yOffset = (float) Math.sin((i * (0.5F + Minecraft.getInstance().pausePartialTick) + (float) milliTime() / 200.0F));
                    int c = p_273375_ & 0xFF000000 | Color.HSBtoRGB(colorr, 0.15F, 1.0F);
                    posX = super.drawInBatch(String.valueOf(drawText.charAt(i)), posX, p_273254_ + yOffset, c, p_273674_, p_273525_, p_272624_, p_273418_, p_273330_, p_272981_);
                    colorr += colorrStep;
                    colorr %= 1.0F;
                }
                return (int) posX;
            }
            return super.drawInBatch(p_273262_, p_273006_, p_273254_, p_273375_, p_273674_, p_273525_, p_272624_, p_273418_, p_273330_, p_272981_);
        }
    };
}
